<template>
	<cu-custom :isBack="true">
		<template #title>问题反馈</template>
	</cu-custom>
	<view class="page-content">
		<view class="text-in default-box theme-shadow" style="margin-bottom: 88rpx">
			<textarea
				placeholder="请输入反馈问题"
				maxlength="500"
				cols="30"
				rows="10"
				style="height:200rpx"
				v-model="textarea">
			</textarea>
			<!-- #ifdef MP-ALIPAY -->
			<view class="voice-in" @click="navToVoice('totext')">
				<text class="cuIcon-voicefill text-gray"></text>
			</view>
			<!-- #endif -->
			<!-- #ifdef H5 -->
			<ToText @updateText="updateText" />
			<!-- #endif -->
		</view>
		
		<!-- <view class="default-box" style="word-break: break-all;">
			内容：{{ voiceResult }}
		</view> -->
	</view>
	
	<view class="mb-4">
		<CuButton size="small" @click="handleSubmit" :disabled="submitdisable">提交</CuButton>
	</view>
</template>

<script setup lang="ts">
import CuButton from "@/components/CuButton.vue"
import { onLoad } from "@dcloudio/uni-app"
import { type Ref, ref } from "vue"
import { createFeedback } from "@/api/user.js"
import { router } from "@/router"
import { obj2str } from "@/utils/base"
import store from "@/store/index.js"
import env from "@/env"
import { voice2Text } from "@/utils/common"
import ToText from "@/components/ToText.vue"
// #ifdef H5
import dd from "gdt-jsapi"
// #endif

const textarea:Ref<string> = ref("")
let to_text_media_id = null

const navToVoice = (recording_type: string) => {
	const query = obj2str({
		recording_type,
		callback: "/pages/user/feedback"
	})
	dd.openLink({
		url: `${env.DD_SCHEME}${env.DD_HOST}/action/open_app?type=1&offline=false&url=${encodeURIComponent(env.MINI_APP_URL + "pages/index/recording?" + query)}`
	}).then((res:any) => {
		console.log("success")
		console.log(res)
	}).catch((err:any) => {
		console.log("error")
		console.log(err)
	})
}
/**
 * 语音文件转文字
 */
const media2Text = ():void => {
	voice2Text(to_text_media_id)
		.then((res) => {
			textarea.value = res
		})
}

const updateText = (e: string) => {
	textarea.value = e
}

const submitdisable:Ref<boolean> = ref(false)

const handleSubmit = () => {
	if (textarea.value.length < 10) {
		uni.showToast({
			icon: "none",
			title: "请至少输入10个字符"
		})
		return
	}
	if (submitdisable.value) return
	submitdisable.value = true
	uni.showLoading({
		title: "提交中..."
	})
	
	createFeedback({
		content: textarea.value
	})
		.then((res) => {
			uni.hideLoading()
			uni.showToast({
				icon: "success",
				title: "提交成功"
			})
			router.back()
		})
		
}

onLoad(async () => {
	const query = await store.dispatch("getQuery")
	if (query.mediaId) {
		to_text_media_id = query.mediaId
		media2Text()
	}
	
})
</script>

<style lang="stylus" scoped>
.text-in
	position: relative
	.voice-in
		position: absolute
		top: 46rpx
		right: 50rpx
		font-size 38rpx
		z-index: 99
</style>